<?php
// index.php?mod=question&childmod=personal&ajax=true&op=createQuestion
$op = get_get("op");
if ($op == "") {
    returnMessage("-1", "unknownrequest");
}
if ($op == "createQuestion") {
    if (! is_Teacher(getSessionUid())) {
        printMessage("error", "只有教师可以进入这里");
        die();
    }
    $setting = array();
    if (isset($_GET["success"]))
        $setting["showSuccMessage"] = true;
    else
        $setting["showSuccMessage"] = false;

    $setting["isNew"] = true;
    $setting["subject"] = getTeacherSubject(getSessionUid(), false);
    setSetting("question_editor", $setting);
    loadTemp("questionbank/personal_view/question_editor.php");
    die();
}
if ($op == "mylist") {
    if (! is_Teacher(getSessionUid())) {
        printMessage("error", "只有教师可以进入这里");
        die();
    }
    $count = countQuestionByUid(getSessionUid());
    $subject = getTeacherSubject(getSessionUid(), false);
    $setting["count"] = $count;
    $setting["hasQuestion"] = $count == 0;
    $setting["subject"] = $subject;
    setSetting("mylist", $setting);
    loadTemp("questionbank/personal_view/my_list.php");
    die();
}
if ($op == "editquestion") {
    $qid = get_get("qid");
    if (! $qid)
        returnMessage("无效的请求，没填写QuestionID");
    $data = getQuestion($qid);
    if (! $data)
        returnMessage("无效的请求，无法查询到您请求的问题");
    $uid = $data["uid"];
    if ($uid != getSessionUid())
        returnMessage("只能作者对该试题进行编辑");
    $setting = array();
    $setting["isNew"] = false;
    $setting["qid"] = $data["questionid"];
    $setting["subject"] = getTeacherSubject(getSessionUid(), false);
    $setting["data"] = $data;
    setSetting("question_editor", $setting);
    loadTemp("questionbank/personal_view/question_editor.php");
    die();
}
if ($op == "uploadBigFileDemo") {
    loadTemp("questionbank/personal_view/uploadbigfiledemo.php");
    die();
}
if ($op == "updateedit") {
    $uid = getSessionUid();
    if (! is_Teacher($uid)) {
        returnMessage(- 10, "你不是老师，没权限操作这个");
    }
    $sid = getTeacherSubject($uid, false)["sid"];
    $type = get_post("isCreate");
    if (! $type) {
        returnMessage(- 1, "类型不正确 $type");
    }

    if ($type == "true") {
        $s_type = get_post("type");
        if (! $s_type)
            returnMessage(- 11, "请填写题目分类，否则题目将不会被检索到");
        $description = get_post("description");
        if (! $description)
            returnMessage(- 2, "叙述不能为空");
        $view_level = get_post("view_level");
        $question = array();
        $q_header = get_post("q_header");
        $answer_view_level = get_post("answer_view_level");
        $explain_view_level = get_post("explain_view_level");
        if (! $q_header)
            returnMessage(- 2, "题干不能为空");
        $q_type = get_post("q_type");
        if ($q_type == 0) {
            $choise_amount = get_post("choise_amount");
            $i = 0;
            while ($i < $choise_amount) {
                $i ++;
                $ch = get_post("choise$i");
                if (! $ch)
                    returnMessage(- 3, "选项$i 是空的");
                $question["choises"][$i] = $ch;
            }
        }
        $question["type"] = $q_type;
        $question["header"] = base64_encode($q_header);
        $question["appends"]["image"] = get_post("question_appends_image");
        $question["appends"]["video"] = get_post("question_appends_video");
        $question_answer = "";
        if ($type == 0) {
            $question_answer = get_post("question_answer_choose");
        }
        if ($type == 1) {
            $question_answer = get_post("question_answer_judze");
        }
        if ($type == 2) {
            $question_answer = get_post("question_answer_text");
            if (! $question_answer)
                returnMessage(- 5, "你没写主观题答案啊$type");
        }
        $qexplain = array();
        $q_explore = get_post("explore");
        $qexplain["explain"] = base64_encode($q_explore);
        $qexplain["appends"]["image"] = get_post("explore_appends_image");
        $qexplain["appends"]["video"] = get_post("explore_appends_video");
        $qid = addQuestion($uid, $sid, $description, $question, $question_answer, $view_level, $qexplain, array(), $s_type, $answer_view_level, $explain_view_level);
        returnMessage(0, "创建成功", array(
            "op" => "reset"
        ));
    } else {
        $qid = get_post("qid");
        if (! $qid)
            returnMessage(- 12, "无效的Qid");
        $ques = getQuestion($qid);
        if (! $ques)
            returnMessage(- 13, "无效的qid");
        if ($ques["uid"] != getSessionUid())
            returnMessage(- 14, "只能由本人编辑这块");

        $s_type = get_post("type");
        if (! $s_type)
            returnMessage(- 11, "请填写题目分类，否则题目将不会被检索到");
        $description = get_post("description");
        if (! $description)
            returnMessage(- 2, "叙述不能为空");
        $view_level = get_post("view_level");
        $question = array();
        $answer_view_level = get_post("answer_view_level");
        $explain_view_level = get_post("explain_view_level");
        $q_header = get_post("q_header");
        if (! $q_header)
            returnMessage(- 2, "题干不能为空");
        $q_type = get_post("q_type");
        if ($q_type == 0) {
            $choise_amount = get_post("choise_amount");
            $i = 0;
            while ($i < $choise_amount) {
                $i ++;
                $ch = get_post("choise$i");
                if (! $ch)
                    returnMessage(- 3, "选项$i 是空的");
                $question["choises"][$i] = $ch;
            }
        }
        $question["type"] = $q_type;
        $question["header"] = base64_encode($q_header);
        $question["appends"]["image"] = get_post("question_appends_image");
        $question["appends"]["video"] = get_post("question_appends_video");
        $question_answer = "";
        if ($type == 0) {
            $question_answer = get_post("question_answer_choose");
        }
        if ($type == 1) {
            $question_answer = get_post("question_answer_judze");
        }
        if ($type == 2) {
            $question_answer = get_post("question_answer_text");
            if (! $question_answer)
                returnMessage(- 5, "你没写主观题答案啊$type");
        }
        $qexplain = array();
        $q_explore = get_post("explore");
        $qexplain["explain"] = base64_encode($q_explore);
        $qexplain["appends"]["image"] = get_post("explore_appends_image");
        $qexplain["appends"]["video"] = get_post("explore_appends_video");
        $qid = updateQuestion($qid, $description, $question, $question_answer, $view_level, $qexplain, array(), $s_type, $answer_view_level, $explain_view_level);

        returnMessage(0, "更新成功", array(
            "op" => "none",
            "label" => $s_type
        ));
        die();
    }
    returnMessage(- 2, "unknown");
}
if ($op == "search_request") {
    if (! is_Teacher(getSessionUid())) {
        returnMessage("-1", "只有教师可以进入这里");
        die();
    }
    $round = "按下列查找内容显示";
    $search_round = "";
    $orderby = get_post("orderby");
    if ($orderby != "lastEditDate" && $orderby != "createdate") {
        $orderby = "createdate";
    }
    $view_level = get_post("view_level");
    $view_chinese = "";
    $displaysearch = get_post("displaysearch");
    $labelsearch = get_post("labelsearch");
    if ($displaysearch == "")
        $displaysearch = "-1";
    if ($labelsearch == "")
        $labelsearch = "-1";
    $search_round .= "orderby=$orderby";
    $search_round .= "&view_level=$view_level";
    $search_round .= "&displaysearch=$displaysearch";
    $search_round .= "&labelsearch=$labelsearch";
    $search_round .= "&index=";
    $back = array();
    $back["op"] = "chengemode";
    $back["round"] = $round;
    $back["search_round"] = $search_round;
    returnMessage(3, "succ", $back);
}
if ($op == "search_load_more") {
    if (! is_Teacher(getSessionUid())) {
        returnMessage("-1", "只有教师可以进入这里");
        die();
    }
    $init = get_post("init");
    if (! $init) {
        $view_level = get_post("view_level");
        $description = get_post("displaysearch");
        $label = get_post("labelsearch");
        $index = get_post("index");
        $orderby = get_post("orderby");
        $amount = 5;
        $data = question_search(getSessionUid(), $index, 10, $view_level, $orderby, $description, $label);
    } else {
        $index = get_post("index");
        $data = question_search(getSessionUid(), $index);
    }
    $back = array();
    $back["bottom"] = false;
    $amount = count($data);
    $back["amount"] = $amount;
    $back["data"] = $data;
    $back["op"] = "load_more";
    if ($amount < 10) {
        $back["bottom"] = true;
    }
    returnMessage(3, "succ", $back);
}
returnMessage("-1", "unknownrequest$op");
?>